package epam.bookShelf.web;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SecurityFilter implements Filter {

	public void init(FilterConfig filterConfig) throws ServletException {
		//servletContext = filterConfig.getServletContext();
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException 
	{
		final HttpServletRequest req = (HttpServletRequest) request;
		final HttpServletResponse resp = (HttpServletResponse) response;

		if (!Boolean.TRUE.equals(req.getSession(true).getAttribute("auth"))
				&& !req.getRequestURI().equals("/login.do") 
				&& !req.getRequestURI().equals("/login.html")
				&& !req.getRequestURI().equals("/styles.css")) 
		{
			System.out.println("Filter has you");
			resp.sendRedirect("/login.html");
			return;
		}

		chain.doFilter(request, response);
	}

	public void destroy() {
		
	}
}
